package thermologic.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Contains a datalogger info
 * and its samples
 * 
 * @author Mauricio
 */
public class Datalogger extends Device implements Serializable, Cloneable
{
    /**
     * Custom data in value=key format
     */
    private Properties customData;
    
    /**
     * Datalogger channels
     */
    private List<Channel> channels;
    
    /**
     * Sinchronize with PC clock
     */
    private boolean synchronizeWithClock;
    
    /**
     * Default constructor required for 
     * XML serialization
     */
    public Datalogger()
    {   
    }
    
    /**
     * Buidls a datalogger with id and description,
     * custom data and channels are empty.
     *  
     * @param id
     * @param description
     */
    public Datalogger(
            String id,
            DeviceType deviceType,
            String description,
            DeviceStatus status,
            boolean synchronizeWithClock)
    {
        super(id, deviceType, description, status);
        this.synchronizeWithClock = synchronizeWithClock;
        customData = new Properties();
        channels = new ArrayList<Channel>();
    }

    /**
     * Check the synchronize with clock flag
     * @return
     */
    public boolean isSynchronizeWithClock()
    {
        return synchronizeWithClock;
    }
    
    /**
     * Set the synchronize with clock flag
     * 
     * @param synchronizeWithClock
     */
    public void setSynchronizeWithClock(boolean synchronizeWithClock)
    {
        this.synchronizeWithClock = synchronizeWithClock;
    }

    /**
     * 
     * @return the custom data
     */
    protected Properties getCustomData()
    {
        return customData;
    }
    
    /**
     * Adds a channel to datalogger
     * 
     * @param channel
     */
    public void addChannel(Channel channel)
    {
        if(channel == null)
        {
            throw new IllegalArgumentException("Channel to add is null");
        }
        
        channels.add(channel);
    }
    
    /**
     * @return and inmuntable channel array
     */
    public Channel[] getChannels()
    {
        Channel channelArray[] = new Channel[channels.size()];
        int i=0;
        
        for(Channel channel: channels)
        {
            final Channel channelTmp = channel;
            channelArray[i++] = channelTmp;
        }
        
        return channelArray;
    }
    
    @Override
    public String toString()
    {
        return getId();
    }
    
    @Override
    public Object clone()
    {
        Datalogger newDatalogger = null;
        Channel newChannel = null;
        
        newDatalogger = new Datalogger(
                getId(),
                getDeviceType(),
                getDescription(),
                getStatus(),
                true
                );
        
        for(Channel c: getChannels())
        {
            newChannel = new Channel(
                    c.isChannelEnnabled(),
                    c.getLabel(),
                    c.getSampleRate(),
                    c.getStartTime(),
                    c.getEndTime(),
                    c.getAlarmLow(),
                    c.getAlarmHigh(),
                    c.getMinValue(),
                    c.getMaxValue(),
                    c.getResolution(),
                    c.getAvailableResolutions(),
                    c.getUnit(),
                    c.getSampleRateUnit());
            
            newDatalogger.addChannel(newChannel);
        }
        
        return newDatalogger;
    }
            
}
